/**
 * @author LYJ
 * Created by Administrator on 2018/3/1 0001.
 */
function FoodTypeForm(option) {
    BootstrapValidateBaseForm.call(this,option.$modal.find("form"),option.$error);
    var This = this;
    this.table = option.table;
    this.$modal = option.$modal;

    /**
     * 清除表单内容
     */
    this.clear = function () {
        this.param("picurl").next("img").attr("src","");
        this.$form[0].reset();
        this.$form.data('bootstrapValidator').resetForm();
    };

    this.init = {
        start: function () {
            This.$error.hintModal();
            This.$modal.hintModal({backdrop:true});
            if (This.type == null || This.type == "add") {
                this.add();
            } else if (This.type == "update") {
                this.update();
            }
            This.element("back").bind("click",function () {
                This.$modal.hintModal().hide();
            });
            This.element("submit").bind("click",function () {
                This.run();
            });

            /** 隐藏modal时事件 */
            This.$modal.on('hide.bs.modal', function () {
                This.clear();
            });
        },
        add: function () {
            This.$form.bootstrapValidator({
                message: '无效',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                live: "enabled",
                fields: {
                    typename: {
                        validators: {
                            notEmpty: {
                                message: '分类名不能为空'
                            },
                            remote: {
                                type:"post",
                                url:"foodtype/validateFoodTypeName",
                                message: '分类已存在'
                            }
                        }
                    }
                }
            });
        },
        update: function () {
            This.$form.bootstrapValidator({
                message: '无效',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                live: "enabled",
                fields: {
                    typename: {
                        validators: {
                            notEmpty: {
                                message: '分类名不能为空'
                            },
                            remote: {
                                type:"post",
                                url:"foodtype/validateFoodTypeName",
                                message: '分类已存在',
                                data:function (va) {
                                    return {
                                        typeid:This.param("typeid").val()
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    };
    
    this.ajaxForm = {
        start: function () {
            This.validate();
            setTimeout(function () {
                if(!This.$form.data("bootstrapValidator").isValid()){
                    return;
                }

                if (This.type == null || This.type == "add") {
                    This.ajaxForm.add();
                } else if (This.type == "update") {
                    This.ajaxForm.update();
                }
            },500);
        },
        add: function () {
            This.element("submit").attr("disabled",true);
            This.$form.ajaxSubmit({
                type: "post",
                url: "foodtype/add",
                contentType : "application/x-www-form-urlencoded; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if(data["recode"] == "0"){
                        This.$modal.hintModal().hide();
                        This.table.tableBar.bootstrapTable("refresh");
                        This.element("submit").attr("disabled",false);
                        This.$error.hintModal().showError("添加成功",1000);
                    }
                }
            });
        },
        update: function () {
            This.element("submit").attr("disabled",true);
            This.$form.ajaxSubmit({
                type: "post",
                url: "foodtype/update",
                contentType : "application/x-www-form-urlencoded; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if(data["recode"] == "0"){
                        This.$modal.hintModal().hide();
                        This.table.tableBar.bootstrapTable("refresh");
                        This.element("submit").attr("disabled",false);
                        This.$error.hintModal().showError("修改成功",1000);
                    }else if(data["recode"] == "1"){
                        this.$form.data("bootstrapValidator").validateField("typename");
                    }else{
                        This.$error.hintModal().showError(data["error"],1000);
                    }
                }
            });
        }
    };
}(function () {
    var Super = function(){};
    Super.prototype = BootstrapValidateBaseForm.prototype;
    FoodTypeForm.prototype = new Super();
})();